﻿package igz.fleaxy.ui.calendar 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import igz.fleaxy.ui.text.LabelUi;
	import igz.fleaxy.util.DateTimeTypes;
	import igz.fleaxy.util.DateTimeUtil;
	/**
	 * ...
	 * @author 
	 */
	public class CalendarNavigatorUi extends Sprite{
		
		protected var _CalendarUi : CalendarUi;
		
		protected var _Tittle : LabelUi;
		protected var _Prev : Sprite;
		protected var _Next : Sprite;
		
		public function CalendarNavigatorUi( p_calendar : CalendarUi, p_prev : Sprite, p_next : Sprite, p_style:String = "calendarDays" ) {
			_CalendarUi = p_calendar;
			
			var s:String = DateTimeUtil.$GetValue( _CalendarUi.$Date, DateTimeTypes.MONTH_NAME );
			
			
			_Tittle = new LabelUi( s, p_style );
			addChild( _Tittle );
			
			_Next = p_next;
			_Next.buttonMode = true;
			addChild( _Next );
			
			_Prev = p_prev;
			_Prev.buttonMode = true;
			addChild( _Prev );
			
			_Draw( _CalendarUi.$Date );
			
			_Prev.addEventListener(MouseEvent.CLICK, _GoPreviousMonth );
			_Next.addEventListener(MouseEvent.CLICK, _GoNextMonth );
		}
		
		protected function _GoPreviousMonth(p_mouseEvent:MouseEvent) : void {
			var date:Date = DateTimeUtil.$Clone( _CalendarUi.$Date );
			date.setMonth( date.getMonth() - 1 );
			
			_Draw( date );
			_CalendarUi.$GoPreviousMonth();
			
		}
		protected function _GoNextMonth(p_mouseEvent:MouseEvent) : void {
			var date:Date = DateTimeUtil.$Clone( _CalendarUi.$Date );
			date.setMonth( date.getMonth() + 1 );
			
			_Draw( date );
			_CalendarUi.$GoNextMonth();
			
		}
		
		protected function _Draw( p_date : Date ) : void {
			var month:String = DateTimeUtil.$GetValue( p_date, DateTimeTypes.MONTH_NAME );
			
			_Tittle.htmlText = month;
			
			_Prev.x = 0;
			_Prev.y = height - _Prev.height >> 1;
			
			_Tittle.x = _Prev.width + 2;
			_Tittle.y = height - _Tittle.height >> 1;
			
			_Next.x = _Tittle.x + _Tittle.width + 2;
			_Next.y = height - _Next.height >> 1;
			
			
			this.graphics.clear();
			this.graphics.beginFill( 0xFF, 0 );
			this.graphics.drawRect( 0, 0, this.width, this.height );
			this.graphics.endFill();
		}
		
		public function $OcultarNavigation():void
		{
			_Prev.visible = false;
			_Next.visible = false;
		}
		
		public function _AlignMonth(p_align:String):void {
			var ancho:uint = _Tittle.width / _Tittle.text.length;
			ancho = ancho * 12;
			if (p_align == "center") {
				_Tittle.x =  (this.width - _Tittle.width) / 2;
				_Prev.x = (this.width - ancho) / 2 - _Prev.width;
				_Next.x = (this.width - ancho) / 2 + ancho;
			} else if (p_align == "right") {
				_Next.x = this.width - _Next.width;
				_Tittle.x = _Next.x - _Tittle.width;	
				_Prev.x = _Tittle.x - _Prev.width;
			}
		}
		
	}

}